/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.abstractvoid.gui;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JTable;
import pl.abstractvoid.distributions.AbstractDistribution;

/**
 * Component responsible for displaying the distribution output. It contains
 * JTable bound to the specific distribution.
 * @author Ivo Zieliński
 */
public class DataOutputPanel extends javax.swing.JPanel
        implements DistributionStateObserver {

    private final MainFrame mediator;
    /**
     * Bounds distribution to its corresponding output table
     */
    HashMap<AbstractDistribution, JTable> outputs = new HashMap();
    JTable currentOutputTable;

    /**
     * Creates new form WynikiPanel
     *
     * @param mediator MainFrame component
     */
    public DataOutputPanel(MainFrame mediator) {
        this.mediator = mediator;
        initComponents();
        setVisible(true);
    }

    /**
     * Changed the visible table when notified about the change of distribution
     */
    @Override
    public void distributionChangedNotify() {
        currentOutputTable = outputs.get(mediator.getCurrentDistribution());
        updateComponents();
    }

    /**
     * Creates and adds new JTable corresponding to newly creating distribution.
     */
    @Override
    public void newDistributionNotify() {
        NonEditableSingleParameterTableModel outputModel = new NonEditableSingleParameterTableModel(mediator.getCurrentDistribution().getOutputTable());
        JTable outputTable = new JTable(outputModel);
        outputTable.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                updateTable();
            }
        });
        outputTable.setFillsViewportHeight(true);
        //Add distribution and JTable to the outputs map
        outputs.put(mediator.getCurrentDistribution(), outputTable);
        currentOutputTable = outputTable;
        updateComponents();
    }

    /**
     * Notifies the JTable so it reloads its values. Invoked when new output
     * is computed.
     */
    private void updateTable() {
        updateUI();
    }

    /**
     * Removes the deleted distribution from the map.
     * @param lastInstanceDeleted 
     */
    @Override
    public void distributionDeletedNotify(boolean lastInstanceDeleted) {
        outputs.remove(mediator.getCurrentDistribution());
        if (lastInstanceDeleted) {
            currentOutputTable = new JTable();
            updateComponents();
        }
    }

    /**
     * Notifies the JTable so it reloads its values and switches to a different
     * JTable. 
     */
    private void updateComponents() {
        tableScrollPane.setViewportView(currentOutputTable);
        updateUI();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tableScrollPane = new javax.swing.JScrollPane();
        outputPanelTable = new javax.swing.JTable();

        tableScrollPane.setViewportView(outputPanelTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tableScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tableScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable outputPanelTable;
    private javax.swing.JScrollPane tableScrollPane;
    // End of variables declaration//GEN-END:variables

    /**
     * Deletes and substitutes the outputs map with a new map due to deserialization
     * of distributions (old are deleted, new are inserted in their plance).
     */
    @Override
    public void distributionsLoadedNotify() {
        outputs = new HashMap<>();
    }

    /**
     * Reloads parameters to show the change of the current language.
     */
    void reloadParameters() {
        for (Map.Entry entry : outputs.entrySet()) {
            JTable table = (JTable) entry.getValue();
            ((NonEditableSingleParameterTableModel) table.getModel()).reloadLanguage();
            ((NonEditableSingleParameterTableModel) table.getModel()).fireTableStructureChanged();
        }
    }
}
